home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / GIFLIB12.ARJ / DEV2GIF.C < prev    next >
C/C++ Source or Header  |  1990-10-17  |  17KB  |  513 lines

  1. /*****************************************************************************
  2. *   "Gif-Lib" - Yet another gif library.                     *
  3. *                                         *
  4. * Written by:  Gershon Elber            IBM PC Ver 1.1,    Jun. 1989    *
  5. ******************************************************************************
  6. * Module to dump graphic devices into a GIF file. Current supported devices: *
  7. * 1. EGA, VGA, SVGA (800x600), Hercules on the IBM PC (#define __MSDOS__).   *
  8. * 2. SGI 4D Irix using gl library (#define __SGI_GL__).                 *
  9. * 3. X11 using libX.a (#define __X11__).                     *
  10. ******************************************************************************
  11. * History:                                     *
  12. * 22 Jun 89 - Version 1.0 by Gershon Elber.                     *
  13. * 12 Aug 90 - Version 1.1 by Gershon Elber (added devices).             *
  14. *****************************************************************************/
  15.  
  16. #ifdef __MSDOS__
  17. #include <dos.h>
  18. #include <alloc.h>
  19. #include <graphics.h>
  20. #endif /* __MSDOS__ */
  21.  
  22. #ifdef __SGI_GL__
  23. #include <gl/gl.h>
  24. #endif /* __SGI_GL__ */
  25.  
  26. #ifdef __X11__
  27. #include <X11/Xlib.h>
  28. #endif /* __X11__ */
  29.  
  30. #include <stdio.h>
  31. #include "gif_lib.h"
  32.  
  33. #define PROGRAM_NAME    "GIF_LIBRARY"
  34.  
  35. #define SVGA_SPECIAL    999               /* 800 by 600 Super VGA mode. */
  36.  
  37. static int GraphDriver = -1,       /* Device parameters - reasonable values. */
  38.        GraphMode = -1,
  39.        ScreenColorBits = 1;
  40. static long ScreenXMax = 100,
  41.         ScreenYMax = 100;
  42. static unsigned int ScreenBase;
  43.  
  44. #ifdef SYSV
  45. static char *VersionStr =
  46.         "Gif library module,\t\tGershon Elber\n\
  47.     (C) Copyright 1989 Gershon Elber, Non commercial use only.\n";
  48. #else
  49. static char *VersionStr =
  50.     PROGRAM_NAME
  51.     "    IBMPC "
  52.     GIF_LIB_VERSION
  53.     "    Gershon Elber,    "
  54.     __DATE__ ",   " __TIME__ "\n"
  55.     "(C) Copyright 1989 Gershon Elber, Non commercial use only.\n";
  56. #endif /* SYSV */
  57.  
  58. #if defined(__SGI_GL__) || defined(__X11__)
  59.     GifByteType *GlblGifBuffer = NULL, *GlblGifBufferPtr = NULL;
  60. #endif /* __SGI_GL__ || __X11__ */
  61.  
  62. #ifdef __SGI_GL__
  63. static int QuantizeRGBBuffer(int Width, int Height, long *RGBBuffer,
  64.                  GifColorType *ColorMap, GifByteType *GIFBuffer);
  65. #endif /* __SGI_GL__ */
  66.  
  67. static void GetScanLine(GifPixelType *ScanLine, int Y);
  68. static int HandleGifError(GifFileType *GifFile);
  69.  
  70. /******************************************************************************
  71. * Dump the given Device, into given File as GIF format:                  *
  72. * Return 0 on success, -1 if device not supported, or GIF-LIB error number.   *
  73. * Device is selected via the ReqGraphDriver. Device mode is selected via      *
  74. * ReqGraphMode1/2 as follows:                              *
  75. * 1. IBM PC Hercules card: HERCMONO (one mode only) in ReqGraphMode1,          *
  76. *    ReqGraphMode2/3 are ignored.                          *
  77. * 2. IBM PC EGA card: EGALO/EGAHI in ReqGraphMode1,                  *
  78. *    ReqGraphMode2/3 are ignored.                          *
  79. * 3. IBM PC EGA64 card: EGA64LO/EGA64HI in ReqGraphMode1,              *
  80. *    ReqGraphMode2/3 are ignored.                          *
  81. * 4. IBM PC EGAMONO card: EGAMONOHI (one mode only) in ReqGraphMode1,          *
  82. *    ReqGraphMode2/3 are ignored.                          *
  83. * 5. IBM PC VGA card: VGALO/VGAMED/VGAHI in ReqGraphMode1,              *
  84. *    ReqGraphMode2/3 are ignored.                          *
  85. * 6. IBM PC SVGA card: ReqGraphMode1/2 are both ignored. Fixed mode (800x600  *
  86. *    16 colors) is assumed.                              *
  87. * 7. SGI 4D using GL: window id to dump (as returned by winget()) in          *
  88. *    ReqGraphMode1, ReqGraphMode2/3 are ignored.                  *
  89. * 8. X11: Window id in ReqGraphMode1, Display id in ReqGraphMode2, Color      *
  90. *    map id in  ReqGraphMode3.                              *
  91. ******************************************************************************/
  92. int DumpScreen2Gif(char *FileName, int ReqGraphDriver, int ReqGraphMode1,
  93.                                       int ReqGraphMode2,
  94.                                       int ReqGraphMode3)
  95. {
  96.     int i, j, k;
  97.     GifPixelType *ScanLine;
  98.     GifFileType *GifFile;
  99.     GifColorType *ColorMap = NULL;
  100. #ifdef __MSDOS__
  101.     static GifColorType MonoChromeColorMap[] = {
  102.         { 0, 0, 0 },
  103.         { 255, 255, 255 }
  104.     };
  105.     /* I have no idea what default EGA64 (4 colors) should be (I guessed...).*/
  106.     static GifColorType EGA64ColorMap[] = {
  107.         {   0,   0,   0 },   /* 0. Black */
  108.         { 255,   0,   0 },   /* 1. Red */
  109.         {   0, 255,   0 },   /* 2. Green */
  110.         {   0,   0, 255 },   /* 3. Blue */
  111.     };
  112.     static GifColorType EGAColorMap[] = {
  113.         {   0,   0,   0 },   /* 0. Black */
  114.         {   0,   0, 170 },   /* 1. Blue */
  115.         {   0, 170,   0 },   /* 2. Green */
  116.         {   0, 170, 170 },   /* 3. Cyan */
  117.         { 170,   0,   0 },   /* 4. Red */
  118.         { 170,   0, 170 },   /* 5. Magenta */
  119.         { 170, 170,   0 },   /* 6. Brown */
  120.         { 170, 170, 170 },   /* 7. LightGray */
  121.         {  85,  85,  85 },   /* 8. DarkGray */
  122.         {  85,  85, 255 },   /* 9. LightBlue */
  123.         {  85, 255,  85 },   /* 10. LightGreen */
  124.         {  85, 255, 255 },   /* 11. LightCyan */
  125.         { 255,  85,  85 },   /* 12. LightRed */
  126.         { 255,  85, 255 },   /* 13. LightMagenta */
  127.         { 255, 255,  85 },   /* 14. Yellow */
  128.         { 255, 255, 255 },   /* 15. White */
  129.     };
  130. #endif /* __MSDOS__ */
  131. #if defined(__SGI_GL__) || defined(__X11__)
  132.     long *RGBBuffer;
  133.     GifColorType ColorMap256[256];
  134. #endif
  135. #ifdef __X11__
  136.     XImage *XImg;
  137.     unsigned long XPixel;
  138.     XColor XColorTable[256];               /* Up to 256 colors in X. */
  139.     XWindowAttributes WinAttr;
  140. #endif /* __X11__ */
  141.  
  142.     switch (ReqGraphDriver) {         /* Return on non supported screens. */
  143. #ifdef __MSDOS__
  144.         case HERCMONO:
  145.         ScreenXMax = 720;
  146.         ScreenYMax = 350;
  147.         ScreenColorBits = 1;
  148.         ScreenBase = 0xb000;
  149.         ColorMap = MonoChromeColorMap;
  150.         break;
  151.     case EGA:
  152.         switch (ReqGraphMode1) {
  153.                 case EGALO:
  154.             ScreenYMax = 200;
  155.             break;
  156.                 case EGAHI:
  157.             ScreenYMax = 350;
  158.             break;
  159.         default:
  160.                     return -1;
  161.         }
  162.         ScreenXMax = 640;
  163.         ScreenColorBits = 4;
  164.         ScreenBase = 0xa000;
  165.         ColorMap = EGAColorMap;
  166.         break;
  167.     case EGA64:
  168.         switch (ReqGraphMode1) {
  169.                 case EGA64LO:
  170.             ScreenYMax = 200;
  171.             break;
  172.                 case EGA64HI:
  173.             ScreenYMax = 350;
  174.             break;
  175.         default:
  176.                     return -1;
  177.         }
  178.         ScreenXMax = 640;
  179.         ScreenColorBits = 2;
  180.         ScreenBase = 0xa000;
  181.         ColorMap = EGA64ColorMap;
  182.         break;
  183.     case EGAMONO:
  184.         switch (ReqGraphMode1) {
  185.                 case EGAMONOHI:
  186.             ScreenYMax = 350;
  187.             break;
  188.         default:
  189.                     return -1;
  190.         }
  191.         ScreenXMax = 640;
  192.         ScreenColorBits = 1;
  193.         ScreenBase = 0xa000;
  194.         ColorMap = MonoChromeColorMap;
  195.         break;
  196.     case VGA:
  197.         switch (ReqGraphMode1) {
  198.                 case VGALO:
  199.             ScreenYMax = 200;
  200.             break;
  201.                 case VGAMED:
  202.             ScreenYMax = 350;
  203.             break;
  204.                 case VGAHI:
  205.             ScreenYMax = 480;
  206.             break;
  207.         default:
  208.                     return -1;
  209.         }
  210.         ScreenXMax = 640;
  211.         ScreenColorBits = 4;
  212.         ScreenBase = 0xa000;
  213.         ColorMap = EGAColorMap;
  214.         break;
  215.         case SVGA_SPECIAL:
  216.         ScreenXMax = 800;
  217.         ScreenYMax = 600;
  218.         ScreenColorBits = 4;
  219.         ScreenBase = 0xa000;
  220.         ColorMap = EGAColorMap;
  221.         break;
  222. #endif /* __MSDOS__ */
  223. #ifdef __SGI_GL__
  224.     case GIF_DUMP_SGI_WINDOW:
  225.         winset(ReqGraphMode1);        /* Select window as active window. */
  226.         getsize(&ScreenXMax, &ScreenYMax);
  227.  
  228.         RGBBuffer = (long *) malloc(sizeof(long) * ScreenXMax * ScreenYMax);
  229.         readsource(SRC_FRONT);
  230.         if (lrectread((short) 0,
  231.               (short) 0,
  232.               (short) (ScreenXMax - 1),
  233.               (short) (ScreenYMax - 1), RGBBuffer) !=
  234.         ScreenXMax * ScreenYMax) {                /* Get data. */
  235.         free(RGBBuffer);
  236.         return -1;
  237.         }
  238.         GlblGifBuffer = (GifByteType *) malloc(sizeof(GifByteType) *
  239.                         ScreenXMax * ScreenYMax);
  240.         i = QuantizeRGBBuffer(ScreenXMax, ScreenYMax, RGBBuffer,
  241.                   ColorMap256, GlblGifBuffer);
  242.         /* Find minimum color map size to hold all quantized colors. */
  243.         for    (ScreenColorBits = 1;
  244.          (1 << ScreenColorBits) < i && ScreenColorBits < 8;
  245.          ScreenColorBits++);
  246.  
  247.         /* Start to dump with top line as GIF expects it. */
  248.         GlblGifBufferPtr = GlblGifBuffer + ScreenXMax * (ScreenYMax - 1);
  249.         ColorMap = ColorMap256;
  250.         free(RGBBuffer);
  251.         break;
  252. #endif /* __SGI_GL__ */
  253. #ifdef __X11__
  254.     case GIF_DUMP_X_WINDOW:
  255.         XGetWindowAttributes((Display *) ReqGraphMode2,
  256.                  (Window) ReqGraphMode1,
  257.                  &WinAttr);
  258.         ScreenXMax = WinAttr.width;
  259.         ScreenYMax = WinAttr.height;
  260.  
  261.         XImg = XGetImage((Display *) ReqGraphMode2,
  262.                  (Window) ReqGraphMode1,
  263.                  0, 0, ScreenXMax - 1, ScreenYMax - 1,
  264.                  AllPlanes, XYPixmap);
  265.  
  266.         GlblGifBuffer = (GifByteType *) malloc(sizeof(GifByteType) *
  267.                         ScreenXMax * ScreenYMax);
  268.  
  269.         /* Scan the image for all different colors exists. */
  270.         for (i = 0; i < 256; i++) XColorTable[i].pixel = 0;
  271.         k = FALSE;
  272.          for (i = 0; i < ScreenXMax; i++)
  273.         for (j = 0; j < ScreenYMax; j++) {
  274.             XPixel = XGetPixel(XImg, i, j);
  275.             if (XPixel > 255) {
  276.             if (!k) {
  277.                 /* Make sure we state it once only.*/
  278.                 fprintf(stderr, "X Color table - truncated.\n");
  279.                 k = TRUE;
  280.             }
  281.             XPixel = 255;
  282.             }
  283.             XColorTable[XPixel].pixel = XPixel;
  284.         }
  285.         /* Find the RGB representation of the colors. */
  286.         XQueryColors((Display *) ReqGraphMode2,
  287.              (Colormap) ReqGraphMode3,
  288.              XColorTable,
  289.              256);
  290.         /* Count number of active colors (Note color 0 is always in)    */
  291.         /* and create the Gif color map from it.                */
  292.         ColorMap = ColorMap256;
  293.         ColorMap[0].Red = ColorMap[0].Green = ColorMap[0].Blue = 0;
  294.         for (i = j = 1; i < 256; i++)
  295.         if (XColorTable[i].pixel) {
  296.             ColorMap[j].Red = XColorTable[i].red / 256;
  297.             ColorMap[j].Green = XColorTable[i].green / 256;
  298.             ColorMap[j].Blue = XColorTable[i].blue / 256;
  299.             /* Save the X color index into the Gif table: */
  300.             XColorTable[i].pixel = j++;
  301.         }
  302.         /* and set the number of colors in the Gif color map. */
  303.         for    (ScreenColorBits = 1;
  304.          (1 << ScreenColorBits) < j && ScreenColorBits < 8;
  305.          ScreenColorBits++);
  306.  
  307.         /* Prepare the Gif image buffer as indices into the Gif color   */
  308.         /* map from the X image.                        */
  309.         GlblGifBufferPtr = GlblGifBuffer;
  310.         for (i = 0; i < ScreenXMax; i++)
  311.         for (j = 0; j < ScreenYMax; j++)
  312.             *GlblGifBufferPtr++ =
  313.             XColorTable[XGetPixel(XImg, j, i) & 0xff].pixel;
  314.         XDestroyImage(XImg);
  315.  
  316.         GlblGifBufferPtr = GlblGifBuffer;
  317.         ColorMap = ColorMap256;
  318.         break;
  319. #endif /* __X11__ */
  320.     default:
  321.         return -1;
  322.     }
  323.  
  324.     ScanLine = (GifPixelType *) malloc(sizeof(GifPixelType) * ScreenXMax);
  325.  
  326.     GraphDriver = ReqGraphDriver;
  327.     GraphMode = ReqGraphMode1;
  328.  
  329.     if ((GifFile = EGifOpenFileName(FileName, FALSE)) == NULL ||
  330.     EGifPutScreenDesc(GifFile, ScreenXMax, ScreenYMax, ScreenColorBits,
  331.               0, ScreenColorBits, ColorMap) == GIF_ERROR ||
  332.     EGifPutImageDesc(GifFile, 0, 0, ScreenXMax, ScreenYMax, FALSE, 1,
  333.              NULL) == GIF_ERROR) {
  334.     free((char *) ScanLine);
  335. #if defined(__SGI_GL__) || defined(__X11__)
  336.     free((char *) GlblGifBuffer);
  337. #endif
  338.     return HandleGifError(GifFile);
  339.     }
  340.  
  341.     for (i = 0; i < ScreenYMax; i++) {
  342.     GetScanLine(ScanLine, i);
  343.     if (EGifPutLine(GifFile, ScanLine, ScreenXMax) == GIF_ERROR) {
  344.         free((char *) ScanLine);
  345. #if defined(__SGI_GL__) || defined(__X11__)
  346.         free((char *) GlblGifBuffer);
  347. #endif
  348.         return HandleGifError(GifFile);
  349.     }
  350.     }
  351.  
  352.     if (EGifCloseFile(GifFile) == GIF_ERROR) {
  353.     free((char *) ScanLine);
  354. #if defined(__SGI_GL__) || defined(__X11__)
  355.     free((char *) GlblGifBuffer);
  356. #endif
  357.     return HandleGifError(GifFile);
  358.     }
  359.  
  360.     free((char *) ScanLine);
  361. #if defined(__SGI_GL__) || defined(__X11__)
  362.     free((char *) GlblGifBuffer);
  363. #endif
  364.     return 0;
  365. }
  366.  
  367. #ifdef __SGI_GL__
  368. /******************************************************************************
  369. * Quantize the given 24 bit (8 per RGB) into 256 colors.              *
  370. ******************************************************************************/
  371. static int QuantizeRGBBuffer(int Width, int Height, long *RGBBuffer,
  372.                   GifColorType *ColorMap, GifByteType *GIFBuffer)
  373. {
  374.     int i;
  375.     GifByteType *RedInput, *GreenInput, *BlueInput;
  376.  
  377.     /* Convert the RGB Buffer into 3 seperated buffers: */
  378.     RedInput = (GifByteType *) malloc(sizeof(GifByteType) * Width * Height);
  379.     GreenInput = (GifByteType *) malloc(sizeof(GifByteType) * Width * Height);
  380.     BlueInput = (GifByteType *) malloc(sizeof(GifByteType) * Width * Height);
  381.  
  382.     for (i = 0; i < Width * Height; i++) {
  383.     RedInput[i] = RGBBuffer[i] & 0xff;
  384.     GreenInput[i] = (RGBBuffer[i] >> 8) & 0xff;
  385.     BlueInput[i] = (RGBBuffer[i] >> 16) & 0xff;
  386.     }
  387.     for (i = 0; i < 256; i++)
  388.     ColorMap[i].Red = ColorMap[i].Green = ColorMap[i].Blue = 0;
  389.  
  390.     i = 256;
  391.     QuantizeBuffer(Width, Height, &i,
  392.            RedInput, GreenInput, BlueInput,
  393.            GIFBuffer, ColorMap);
  394.  
  395.     free(RedInput);
  396.     free(GreenInput);
  397.     free(BlueInput);
  398.  
  399.     return i;               /* Real number of colors in color table. */
  400. }
  401. #endif /* __SGI_GL__ */
  402.  
  403. /******************************************************************************
  404. * Update the given scan line buffer with the pixel levels of the Y line.      *
  405. * This routine is device specific, so make sure you know was you are doing    *
  406. ******************************************************************************/
  407. static void GetScanLine(GifPixelType *ScanLine, int Y)
  408. {
  409.     unsigned char ScreenByte;
  410.     int i, j, k;
  411.     unsigned int BufferOffset, Bit;
  412. #ifdef __MSDOS__
  413.     union REGS InRegs, OutRegs;
  414. #endif /* __MSDOS__ */
  415.  
  416.     switch (GraphDriver) {
  417. #ifdef __MSDOS__
  418.     case HERCMONO:
  419.         BufferOffset = 0x2000 * (Y % 4) + (Y / 4) * (ScreenXMax / 8);
  420.         /* In one scan lines we have ScreenXMax / 8 bytes: */
  421.         for (i = 0, k = 0; i < ScreenXMax / 8; i++) {
  422.         ScreenByte = (unsigned char) peekb(ScreenBase, BufferOffset++);
  423.         for (j = 0, Bit = 0x80; j < 8; j++) {
  424.             ScanLine[k++] = (ScreenByte & Bit ? 1 : 0);
  425.             Bit >>= 1;
  426.         }
  427.         }
  428.         break;
  429.     case EGA:
  430.     case EGA64:
  431.     case EGAMONO:
  432.     case VGA:
  433.     case SVGA_SPECIAL:
  434.         InRegs.x.dx = Y;
  435.         InRegs.h.bh = 0;
  436.             InRegs.h.ah = 0x0d;                   /* BIOS Read dot. */
  437.         for (i = 0; i < ScreenXMax; i++) {
  438.             InRegs.x.cx = i;
  439.         int86(0x10, &InRegs, &OutRegs);
  440.             ScanLine[i] = OutRegs.h.al;
  441.         }
  442.  
  443.         /* Makr this line as done by putting a xored dot on the left. */
  444.         InRegs.x.dx = Y;
  445.         InRegs.h.bh = 0;
  446.         InRegs.h.ah = 0x0c;               /* BIOS Write dot (xor mode). */
  447.         InRegs.h.al = 0x81;                    /* Xor with color 1. */
  448.         InRegs.x.cx = 0;
  449.         int86(0x10, &InRegs, &OutRegs);
  450.         InRegs.x.dx = Y;
  451.         InRegs.h.bh = 0;
  452.         InRegs.h.ah = 0x0c;               /* BIOS Write dot (xor mode). */
  453.         InRegs.h.al = 0x81;                    /* Xor with color 1. */
  454.         InRegs.x.cx = 1;
  455.         int86(0x10, &InRegs, &OutRegs);
  456.  
  457.         if (Y == ScreenYMax - 1) {/* Last row - clear all marks we made. */
  458.         for (i = 0; i < ScreenYMax; i++) {
  459.             InRegs.h.bh = 0;
  460.             InRegs.h.ah = 0x0c;           /* BIOS Write dot (xor mode). */
  461.             InRegs.h.al = 0x81;           /* Xor back with color 1. */
  462.             InRegs.x.dx = i;
  463.             InRegs.x.cx = 0;
  464.             int86(0x10, &InRegs, &OutRegs);
  465.             InRegs.h.bh = 0;
  466.             InRegs.h.ah = 0x0c;           /* BIOS Write dot (xor mode). */
  467.             InRegs.h.al = 0x81;           /* Xor back with color 1. */
  468.             InRegs.x.dx = i;
  469.             InRegs.x.cx = 1;
  470.             int86(0x10, &InRegs, &OutRegs);
  471.                 }
  472.         }
  473.         break;
  474. #endif /* __MSDOS__ */
  475. #ifdef __SGI_GL__
  476.     case GIF_DUMP_SGI_WINDOW:
  477.         memcpy(ScanLine, GlblGifBufferPtr, ScreenXMax * sizeof(GifPixelType));
  478.         GlblGifBufferPtr -= ScreenXMax;
  479.         break;
  480. #endif /* __SGI_GL__ */
  481. #ifdef __X11__
  482.     case GIF_DUMP_X_WINDOW:
  483.         memcpy(ScanLine, GlblGifBufferPtr, ScreenXMax * sizeof(GifPixelType));
  484.         GlblGifBufferPtr += ScreenXMax;
  485.         break;
  486. #endif /* __X11__ */
  487.     default:
  488.         break;
  489.     }
  490. }
  491.  
  492. /******************************************************************************
  493. * Handle last GIF error. Try to close the file and free all allocated memory. *
  494. ******************************************************************************/
  495. static int HandleGifError(GifFileType *GifFile)
  496. {
  497.     int i = GifLastError();
  498.  
  499.     if (EGifCloseFile(GifFile) == GIF_ERROR) {
  500.     GifLastError();
  501.     }
  502.     return i;
  503. }
  504.  
  505.  
  506.  
  507.  
  508.  
  509.  
  510.  
  511.  
  512.  
  513.